class Solution {
    //回文子串
    public int countSubstrings(String s) {
        int n = s.length();
        char[] chs = s.toCharArray();
        boolean[][] dp = new boolean[n][n];
        int ret = 0;
        for(int i = n-1; i >= 0; i--){
            for(int j = n-1; j >= i; j--){
                if(chs[i] == chs[j]){
                    if(i == j || i+1 == j) dp[i][j] = true;
                    else dp[i][j] = dp[i+1][j-1];
                }
                if(dp[i][j]) ret++;
            }
           
        }
        return ret;
    }
    //最长回文串
    public String longestPalindrome(String s) {
        int n = s.length();
        char[] chs = s.toCharArray();
        int begin = 0, end = 0;
        boolean[][] dp = new boolean[n][n];
        for(int i = n - 1; i >= 0; i--){
            for(int j = n - 1; j >= i; j--){
                if(chs[i] == chs[j]){
                    if(i == j || i + 1 == j) dp[i][j] = true;
                    else dp[i][j] = dp[i+1][j-1];
                    if(dp[i][j] && j - i + 1 > end - begin + 1){
                        begin = i;
                        end = j;
                    }
                }
            }
        }
        return s.substring(begin, end+1);
    }
    //回文串分割IV
    public boolean checkPartitioning(String s) {
        int n = s.length();
        char[] chs = s.toCharArray();
        boolean[][] dp = new boolean[n][n];
        for(int i = n - 1; i >= 0; i--){
            for(int j = n - 1; j >= i; j--){
                if(chs[i] != chs[j]) continue;
                if(i == j || i + 1 == j) dp[i][j] = true;
                else dp[i][j] = dp[i+1][j-1];
            }
        }
        for(int i = 1; i < n - 1; i++){
            for(int j = i; j < n - 1; j++){
                if(dp[0][i - 1] && dp[i][j] && dp[j + 1][n - 1]) return true;
            }
        }
        return false;
    }
    //分割字符串II
    public int minCut(String s) {
        int n = s.length();
        //  if(n <= 1) return 0;
        char[] chs = s.toCharArray();
        boolean[][] dp = new boolean[n][n];
        for(int i = n - 1; i >= 0; i--){
            for(int j = n - 1; j >= i; j--){
                if(chs[i] != chs[j]) continue;
                if(i == j || i + 1 == j) dp[i][j] = true;
                else dp[i][j] = dp[i+1][j-1];
            }
        }
        int[] f = new int[n];
        for(int i = 0; i < n; i++){
            f[i] = Integer.MAX_VALUE;
        }
        for(int i = 0; i < n; i++){
            if(dp[0][i]){
                f[i] = 0;
                continue;
            }else{
                for(int j = 1; j <= i; j++){
                    if(dp[j][i]) f[i] = Math.min(f[j-1] + 1, f[i]);
                }

            }

        }
        return f[n-1];
    }
    //最长回文子序列
    public int longestPalindromeSubseq(String s) {
        int n = s.length();
        int[][] dp = new int[n][n];
        for(int i = n-1; i >= 0; i--){
            for(int j = i; j < n; j++){
                if(s.charAt(i) == s.charAt(j)){
                    if(i == j) dp[i][j] = 1;
                    else dp[i][j] = dp[i+1][j-1] + 2;
                }else{
                    dp[i][j] = Math.max(dp[i][j-1], dp[i+1][j]);
                }
            }
        }
        return dp[0][n-1];
    }
    //让字符串称为回文串的最少插入次数
    public int minInsertions(String s) {
        int n = s.length();
        int[][] dp = new int[n][n];
        for(int i = n-1; i >= 0; i--){
            for(int j = i + 1; j < n; j++){
                if(s.charAt(i) == s.charAt(j)){
                    dp[i][j] = dp[i+1][j-1];
                }else{
                    dp[i][j] = Math.min(dp[i][j - 1] + 1, dp[i + 1][j] + 1);
                }
            }
        }
        return dp[0][n - 1];
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.longestPalindromeSubseq("cbbd");
    }
}